$(document).ready(function () {


    function createDots() {
        for (var i = 1; i <= 7; i++) {
            var div = $('<div class="divDots">').addClass('dot').css('transition', 'right ' + (0.6 + i / 10).toFixed(1) + 's ease-out');
            div.css('top', '0');
            div.prependTo($('#widget_preview'));


        }
    }


    function loadDots() {
        var i = 0;
        animateDots();
        function animateDots() {
            setTimeout(function () {
                var dots = $('div.dot');

                if (dots.length) {
                    dots.toggleClass('open');
                }
                i++;
                if (i < 1) {
                    animateDots();
                } else {
                }
            }, 1300)
        }
    }

    $('#submit').click(function () {
        var attempt = $("#user").hasClass("attempt");
        if (!attempt) {
            createDots();
        }
        loadDots();

        setTimeout(function () {

            if ($('#username').val() == "" || $('#pass').val() == "") {
                $("#user").css("outline", "3px solid rgb(235, 46, 46)");
                $("#user").addClass("attempt");
                $("#loginForm").css("outline", "3px solid rgb(235, 46, 46)");
                $('.divDots').each(function () {
                    //console.log($(this));
                });
                return false;
            }

            var args = { "Username": $('#username').val(), "Password": $('#pass').val() };

            $.ajax({
                type: "POST",
                url: "http://uclwebservicetest.cloudapp.net/Services/users.svc/user/login",
                data: JSON.stringify(args),
                contentType: "application/json; charset=utf-8",
                success: function (response) {

                    $.ajax({
                        url: "scripts/Sessions.aspx",
                        global: false,
                        type: "GET",
                        async: false,
                        data: {
                            Uri: response.href,
                            Country: response.response.Country,
                            EmailAddress: response.response.EmailAddress,
                            FirstName: response.response.FirstName,
                            LastName: response.response.LastName,
                            Organization: response.response.Organization,
                            Purpose: response.response.Purpose,
                            Title: response.response.Title,
                            Id: response.response.Id,
                            Username: response.response.Username
                        },
                        success: function (response) {
                            console.log(response);

                            $("#loginPage").addClass("flipped");
                            var current = window.location.href;
                            var pos = current.lastIndexOf('/');
                            var redirect = current.substring(0, pos) + "/index.aspx";

                            window.location.href = redirect;
                        }
                    });
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $("#user").css("outline", "3px solid rgb(235, 46, 46)");
                    $("#loginForm").css("outline", "3px solid rgb(235, 46, 46)");
                    console.log(jqXHR.status);
                    console.log(textStatus);
                    console.log(errorThrown);

                    console.log("ARGS: %s", JSON.stringify(args));
                }
            });
        }, 2000);
    });

});

